<?php
namespace app\shop\controller;
use think\Controller;
use think\Request;
use think\Db;
use app\service\billService;
use app\service\lifeapiService;//生活号接口

use app\service\shopService;
use app\service\appletService;
use printing\printShop;
class Test extends Controller{
	public function test(){
		$goodlist=Db::table("applet_goods")->select();
		$this->assign("goodlist",$goodlist);
		return View::fetch();
	}
	public function billnotice(){
		//$address = 'C:\phpStudy\WWW\zaozao\public\shop\bill\alinotice\\'.date('YmdHis').rand(100000,999999).'.txt';
		$address='C:\phpStudy\WWW\zaozao\public\shop\bill\alinotice\20190802163918888076.txt';
        //file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);//将数据读入文本
        parse_str(file_get_contents($address),$notice);//读取文本并变成数组
        if($notice['trade_status'] == 'TRADE_SUCCESS'){
        	//$order = (new shopService())->tradeOrderInfo($notice['trade_no']);
        	$billorder=Db::table('zz_bill_order')->where('trade_no',$notice['trade_no'])->find();
        	//更改订单状态
        	if($billorder["order_status"]=="0"){
        		//
        		$data["id"]=$billorder["id"];
        		$data["pay_type"]="1";//支付方式
        		$data['order_status'] = '1';
                $data['pay_time'] = $notice['gmt_payment'];
				$data['pay_money'] = $notice['receipt_amount'];
				$res=Db::table('zz_bill_order')->update($data);
				//如果订单使用积分，则更新房屋的积分
				if($billorder["point_status"]=="1"){
					if($billorder["point"]>0){
						Db::table('roominfo')->where('out_room_id',$billorder["out_room_id"])->setDec('point', $billorder["point"]);
					}
				}
        	}
        	//修改账单状态
        	$billid=unserialize($billorder["bill_id"]);
        	foreach ($billid as $key => $value) {
        		$billinfo=(new billService())->billIdinfo($value);
        		if($billinfo["status"]!="TRADE_SUCCESS"){
        			$info['pay_type'] = '支付宝';//付款类型
	                $info['trade_no'] = $notice['trade_no'];//支付宝交易号
	                $info['gmt_payment'] = $notice['gmt_payment'];//付款时间
	                $info['status'] = $notice['trade_status'];//付款状态
	                $info['id'] = $value;
	                 //修改数据
               		$billedit = (new billService())->billApiEdit($info);
        		} 
        		//删除已同步账单
        		if($billinfo["tongbu"]=="1"){
                    $res=(new appletService())->alibilldel($billinfo['id']);
                    if($res){
                    	 Db::table("bill")->where('id',$billinfo['id'])->update(["tongbu"=>"3"]);
                    }
                } 
                $detailed[] = '费用类型: '.$billinfo['cost_type'];
                $detailed[] = '账期: '.$billinfo['acct_period'];
                $detailed[] = '金额: '.$billinfo['bill_entry_amount']; 
        	}

        	
        	//判断是否转账
        	if(($billorder["point_status"]=="1")&&($billorder["point_money"]>0)){
        		if($billorder["transfer_status"]=="0"){
        			$property=(new shopService())->communityProperty($billorder["community_id"]);
	        		$api = new lifeapiService();
	        		$transfer_rate=0.0038;
					$transfer_data["out_biz_no"]=$notice['trade_no'];
					$transfer_data["payee_type"]="ALIPAY_USERID";
					$transfer_data["payee_account"]=$property["merchantpid"]; //2088231771220793
					$money=$billorder['point_money']*(1-$transfer_rate);
					$money=(int)(ceil($money*100));
		    		$money=$money/100;
					$transfer_data["amount"]=$money;
					$transfer_data["payer_show_name"]=$billorder["logon_id"].$billorder["user_name"];
					$transfer_data["payee_real_name"]=$property["name"];
					$transfer_data["remark"]="番茄会员消费";
					$result=$api->accountTransfer(json_encode($transfer_data));
					var_dump($result);
					//转账成功
					if($result->code=="10000"){
						$transfer["id"]=$billorder["id"];
						$transfer["transfer_status"]=1;
						Db::table('zz_bill_order')->update($transfer);


						//往物业转账记录插入一条记录
						$transfer_bill["community_id"]=$billorder["community_id"];
						$transfer_bill["community_name"]=$billorder["community_name"];
						$transfer_bill["property_account"]=$property["merchantpid"];
						$transfer_bill["property_name"]=$property["name"];
						$transfer_bill["transfer_money"]=$money;
						$transfer_bill["point_money"]=$billorder['point_money'];
						$transfer_bill["out_order_no"]=$billorder['out_order_no'];
						$transfer_bill["trade_no"]=$billorder['trade_no'];
						$transfer_bill["transfer_rate"]=$transfer_rate;
						Db::table("zz_transfer_bill")->insert($transfer_bill);

					}
	        	}
        	}
        	//打印
        	if($billorder["printing"]=="0"){
        		$printdata["id"]=$billorder["id"];
        		$communityinfo = (new billService())->communityMachine($billorder['community_id']);
        		
        		$machine_code="4004619114";
        		//$machine_code=$communityinfo["machine_code"];
        		$printing = (new printShop())->billprinting($machine_code,$billorder["community_name"],$billorder["room_address"],$notice['trade_no'],"支付宝",$notice['receipt_amount'],$notice['gmt_payment'],$billorder["order_money"],$billorder["point_money"],$detailed);
        		var_dump($printing);
        		if($printing){
        			$printdata["printing"]="1";
        			Db::table("zz_bill_order")->update($printdata);
        		}
        	}
        	
        }
	}

	public function uploads(){
        $logo_data = file_get_contents("C:\phpStudy\WWW\zaozao\application\shop\image.txt");
        //$logo_data = $_POST['logo_base64'];
          
        
        //$data = file_get_contents('./1.txt');
        $reg = '/data:image\/(\w+?);base64,(.+)$/si';
        preg_match($reg,$logo_data,$match_result);
        //echo "<pre>";
		//var_dump($match_result);die;
        $file_name = time().'.'.$match_result[1];
         
         $logo_path = 'C:\phpStudy\WWW\zaozao\application\shop\\'.$file_name;
         $num = file_put_contents($logo_path,base64_decode($match_result[2]));
         
         return $num;
     return $result;
     }
	
	public function verityresultmsg(){
		$msg["title"]="恭喜您，您的商品已通过审核";
		$msg["user_id"]="2088112618452297";//"2088102690661313";
		$msg["url"]="http://www.jfzhiwen.com/shop/merchant/goodlist.html";
		$msg["ctime"]=date('Y-m-d H:i:s');
		$msg["state"]="已通过";
		
		$msg["remark"]="test";
		$api=new lifeapiService();
		$res=$api->verityresultmsg(json_encode($msg));
		var_dump($res);
	}
	public function testmsg1(){
		$msg["title"]="尊敬的会员，你有一笔新的积分入账啦";
		$msg["user_id"]="2088112618452297";//"2088102690661313";
		$msg["url"]="";
		$msg["shop_name"]="早早小店";
		$msg["pay_money"]="10"."元";
		$msg["give_point"]="10"."积分";
		$msg["point"]="100"."积分";
		$msg["time"]=date('Y-m-d H:i:s');
		
		$msg["remark"]="";
		$api = new lifeapiService();
        $result = $api->getPointMsg(json_encode($msg));
		var_dump($result);
	}
	public function testmsg2(){
		$data["title"]="您刚才进行了绑定房屋操作";
		$data["user_id"]="2088112618452297";
		$data["url"]="https://www.jfzhiwen.com/shop/room/index";
		$data["time"]=date('Y-m-d H:i:s');
		$data["con"]="绑定房屋：春风里18幢3单元1301室";
		$data["remark"]="";
		$api = new lifeapiService();
        $result = $api->bindRoomMsg(json_encode($data));
		var_dump($result);
	}
	public function test3(){
		
		$data["user_id"]="2088112618452297";
		
		$api = new lifeapiService();
        $result = $api->userFollowQuery(json_encode($data));
		var_dump($result);
	}

	public function test4(){
		$data["out_order_no"]="20190715203705895767";
		$data["trade_no"]="2019071522001407021050584651";
		$data["trans_out"]="2088712715007020";
		$data["trans_out_type"]="userId";
		$data["trans_in_type"]="userId";
		$data["trans_in"]="2088112618452297";
		$data["amount"]="1";
		$data["desc"]="转账到杨战磊";
		$api = new lifeapiService();
		$result=$api->orderSettle(json_encode($data));

		var_dump($result);
	}
	public function getuser(){
		$api = new lifeapiService();
		//获取当前用户支付宝ID
		$code = request()->get('auth_code');
		$oauthtoken = $api->oauthToken($code);
		if(property_exists($oauthtoken,'sub_msg')){
			return json(['info' => $oauthtoken->sub_msg]);
		}
		session('user_id',$oauthtoken->user_id);
        $url=session("url");
        $this->redirect($url);
        die;	
	}

	public function accountTransfer(){
		$api = new lifeapiService();
		$data["out_biz_no"]="2019072036082716001";
		$data["payee_type"]="ALIPAY_USERID";
		$data["payee_account"]="2088112618452297"; //2088231771220793 
		$data["amount"]="0.5";
		$data["payer_show_name"]="杭州早早科技有限公司";
		$data["payee_real_name"]="杨战磊";
		$data["remark"]="1";
		$result=$api->accountTransfer(json_encode($data));
		var_dump($result);
	}

	public function index(){
		$next_user_id="";
        $api = new lifeapiService();
        $result = $api->getuserlist(json_encode(['next_user_id' => $next_user_id ]));
		var_dump($result);
    }

    public function testmenu(){
    	$api = new lifeapiService();
    	$result = $api->modifyMenu();
    	var_dump($result);
    }

}